package com.doyens.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.reggie.commons.R;
import com.doyens.reggie.domain.Category;
import com.doyens.reggie.domain.Setmeal;
import com.doyens.reggie.dto.SetmealDto;
import com.doyens.reggie.service.CategoryService;
import com.doyens.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CacheManager cacheManager;

    // 修改
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }

    // 根据套餐的id查询套餐  做回显
//    @Cacheable(value = "userCache",key = "#id",unless = "#result==null")
    @GetMapping("/{id}")
    public R<SetmealDto> findSetmealById(@PathVariable long id){
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    // 起售和停售
//    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable int status,@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealService.update(setmeal,wrapper);
        return R.success("修改成功");
    }

    // 批量删除

    @DeleteMapping
//    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmeaCache
    public R<String> delSetmeal(@RequestParam List<Long> ids){
        // 批量删除  1  2  3
        setmealService.delWithSetmealDish(ids);
        return R.success("删除成功");
    }

    // 分页查询套餐

    @GetMapping("/page")
    public R<Page<SetmealDto>> getSetmealPage(Long page,Long pageSize,String name){
        //1.创建Page对象 查询setmeal表
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        //2.创建Page对象  保存类型为SetmealDto类型
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //3.通过setmealService调用方法 查询符合条件的 数据
        LambdaQueryWrapper<Setmeal> queryWrapper =  new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        this.setmealService.page(setmealPage,queryWrapper);

        //4.将setmealPage对象中的字段 复制给setmealDtoPage
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        //5.取出setmealPage中的 records字段集合
        List<Setmeal> setmealList = setmealPage.getRecords();
        //6.遍历setmealList集合  对每一个setmeal对象 查询并赋值对应的分类名称
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            //通过 categoryService调用getById方法获取 分类对象
            Category category = this.categoryService.getById(setmeal.getCategoryId());
            //判断category对象是否为空
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        //将 setmealDtoList 放到 setmealDtoPage对象中
        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);
    }

     @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto){
        log.info("添加套餐数据为：" + setmealDto);
        setmealService.saveSetmeal(setmealDto);
        return R.success("添加成功");
    }


    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key ="#p0+'_'+#p1" ,unless ="#result.data.size()==0" )
    public R<List<Setmeal>> getList(Long categoryId,Long status){
        //1.创建条件构造器对象
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        queryWrapper.eq(Setmeal::getStatus,status);
        //通过 setmealService调用方法 查询符合条件的 套餐信息
        List<Setmeal> list = this.setmealService.list(queryWrapper);

        return R.success(list);
    }
}
